package com.ssm.controller;

import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssm.entity.UserVo;
import com.ssm.utils.ImportExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@RestController
@RequestMapping("/uploadExcel")
public class LeadingExcelController {

    @RequestMapping("/form")
    public Map<String, Object> form(HttpServletRequest request)throws Exception{
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        InputStream in =null;
        List<List<Object>> listob = null;
        MultipartFile file = multipartRequest.getFile("upfile");

        if(file.isEmpty()){
            throw new Exception("文件不存在！");
        }
        in = file.getInputStream();
        listob = new ImportExcelUtil().getBankListByExcel(in,file.getOriginalFilename());
        in.close();

        List<UserVo> list = new ArrayList<>();
        //该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
        for (int i = 0; i < listob.size(); i++) {
            List<Object> lo = listob.get(i);
            UserVo family = new UserVo();
            family.setUserId(Long.parseLong(String.valueOf(lo.get(0))));
            family.setUsername(String.valueOf(lo.get(1)));
            family.setPassword(String.valueOf(lo.get(2)));
            family.setEmail(String.valueOf(lo.get(3)));
            list.add(family);
            System.out.println("打印信息-->"+family.toString());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("user", list);

        return map;
    }

    @RequestMapping(value="/ajax")
    public  void  ajaxUploadExcel(HttpServletRequest request,HttpServletResponse response) throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;


        InputStream in =null;
        List<List<Object>> listob = null;
        MultipartFile file = multipartRequest.getFile("upfile");
        if(file.isEmpty()){
            throw new Exception("文件不存在！");
        }

        in = file.getInputStream();
        listob = new ImportExcelUtil().getBankListByExcel(in,file.getOriginalFilename());

        //该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
        for (int i = 0; i < listob.size(); i++) {
            List<Object> lo = listob.get(i);
            UserVo family = new UserVo();
            family.setUserId((Long)lo.get(0));
            family.setUsername(String.valueOf(lo.get(1)));
            family.setPassword(String.valueOf(lo.get(2)));
            family.setEmail(String.valueOf(lo.get(3)));

            System.out.println("打印信息-->"+family.toString());
        }

        PrintWriter out = null;
        response.setCharacterEncoding("utf-8");  //防止ajax接受到的中文信息乱码
        out = response.getWriter();
        out.print("文件导入成功！");
        out.flush();
        out.close();
    }

}